//
//  FGImageMessageCell.m
//  FGChatKit
//
//  Created by Henry on 2021/6/3.
//

#import "FGImageMessageCell.h"
#import "UIView+FGLayout.h"
#import "UIImageView+FGCornerRadius.h"

@implementation FGImageMessageCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _thumbView = [[UIImageView alloc] init];
        [_thumbView fg_cornerRadius:4];
        [self.container addSubview:_thumbView];
        _thumbView.mm_fill();
        _thumbView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

        _progressView = [[FGCircularProgressView alloc] init];
        _progressView.roundedCorners = YES;
        [self.container addSubview:_progressView];
        
    }
    return self;
}

- (void)fillWithData:(FGImageMessageCellData *)data {
    [super fillWithData:data];
    self.imageData = data;
    
    _thumbView.image = nil;
    
    [data loadImage];

    CGFloat progress = self.imageData.thumbProgress / 100.f;
    self.progressView.hidden = (progress >= 1 || progress == 0);
    [self.progressView setProgress:progress animated:YES];

    [self.imageData addObserver:self forKeyPath:@"thumbProgress" options:NSKeyValueObservingOptionNew context:nil];
    [self.imageData addObserver:self forKeyPath:@"thumbImage" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionInitial context:nil];
    if (self.messageData.direction == FGMsgDirectionOutgoing) {
        [self.imageData addObserver:self forKeyPath:@"uploadProgress" options:NSKeyValueObservingOptionNew context:nil];
    }
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
    
    if ([keyPath isEqualToString:@"thumbProgress"]) {
        CGFloat progress = self.imageData.thumbProgress / 100.f;
        NSLog(@"thumbProgress: %lf", progress);
        [self.progressView setProgress:progress animated:YES];
        self.progressView.hidden = (progress >= 1 || progress == 0);
        
        if (progress >= 1) {
            // 修改
            UIImage *oImage = [self.imageData thumbImage];
            if (!oImage) {
                return;
            }
            CGSize size = CGSizeMake(oImage.size.width, oImage.size.height);
            CGSize oSize = self.imageData.items.firstObject.size;
            if (!CGSizeEqualToSize(size, oSize)) {
                self.imageData.items.firstObject.size = size;
                dispatch_async(dispatch_get_main_queue(), ^{
                    if ([self.delegate respondsToSelector:@selector(imageDownloadComplete:)]) {
                        [self.delegate imageDownloadComplete:self];
                    }
                });
            }
        }

    } else if ([keyPath isEqualToString:@"thumbImage"]) {
        self.thumbView.image = self.imageData.thumbImage;
    } else if ([keyPath isEqualToString:@"uploadProgress"]) {
        CGFloat progress = self.imageData.uploadProgress / 100.f;
        NSLog(@"uploadProgress: %lf", progress);
        [self.progressView setProgress:progress animated:YES];
        self.progressView.hidden = (progress >= 1 || progress == 0);
    }

}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.progressView.mm_center();
}

- (void)dealloc {
    if (self.imageData) {
        [self.imageData removeObserver:self forKeyPath:@"thumbProgress" context:nil];
        [self.imageData removeObserver:self forKeyPath:@"thumbImage" context:nil];
        if (self.messageData.direction == FGMsgDirectionOutgoing) {
            [self.imageData removeObserver:self forKeyPath:@"uploadProgress" context:nil];
        }
    }
}

@end
